﻿using Omnia.Infrastructure;
using Omnia.Enums;
using System.Windows.Media;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using System;
using System.ComponentModel;

namespace Omnia.DataModels
{
    public class OptionInfo : NotificationObject
    {
        public OptionInfo()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                CheckSettings();
            }
        }

        #region Fields
        private string backgroundImage = "/Omnia.Wallet;component/Assets/desert.png";
        private string language = "zh-CN";
        private ImageLocationValue location = ImageLocationValue.Applocation;
        private bool autoSavePlace = true;
        private Stretch backgroundImageStretch = Stretch.UniformToFill;
        private int notifyDays = 7;
        #endregion

        #region Properties
        public string BackgroundImage
        {
            get { return backgroundImage; }
            set
            {
                backgroundImage = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.ImageName] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => BackgroundImage);
            }
        }

        public string Language
        {
            get { return language; }
            set
            {
                language = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.Language] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => Language);
            }
        }

        public ImageLocationValue Location
        {
            get { return location; }
            set
            {
                location = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.Location] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => Location);
            }
        }

        public bool AutoSavePlace
        {
            get
            {
                return autoSavePlace;
            }
            set
            {
                autoSavePlace = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.AutoSavePlace] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => AutoSavePlace);
            }
        }

        public Stretch BackgroundImageStretch
        {
            get { return backgroundImageStretch; }
            set
            {
                backgroundImageStretch = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.ImageStretch] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => BackgroundImageStretch);
            }
        }

        public int NotifyDays
        {
            get { return notifyDays; }
            set
            {
                notifyDays = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.NitifyDays] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => NotifyDays);
            }
        }

        public IEnumerable<Stretch> StretchCollection
        {
            get
            {
                yield return Stretch.Fill;
                yield return Stretch.None;
                yield return Stretch.Uniform;
                yield return Stretch.UniformToFill;
            }
        }

        #endregion

        #region Methods
        private void CheckSettings()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            #region Background
            if (!settings.Contains(OptionKeys.ImageName))
            {
                settings.Add(OptionKeys.ImageName, backgroundImage);
                settings.Save();
            }
            else
            {
                BackgroundImage = settings[OptionKeys.ImageName].ToString();
            }
            #endregion

            #region Language
            if (!settings.Contains(OptionKeys.Language))
            {
                settings.Add(OptionKeys.Language, language);
                settings.Save();
            }
            else
            {
                Language = settings[OptionKeys.Language].ToString();
            }
            #endregion

            #region Image Location
            if (!settings.Contains(OptionKeys.Location))
            {
                settings.Add(OptionKeys.Location, location);
                settings.Save();
            }
            else
            {
                Location = (ImageLocationValue)settings[OptionKeys.Location];
            }
            #endregion

            #region AutoSavePlace
            if (!settings.Contains(OptionKeys.AutoSavePlace))
            {
                settings.Add(OptionKeys.AutoSavePlace, autoSavePlace);
                settings.Save();
            }
            else
            {
                AutoSavePlace = (bool)settings[OptionKeys.AutoSavePlace];
            }
            #endregion

            #region BackgroundImageStretch
            if (!settings.Contains(OptionKeys.ImageStretch))
            {
                settings.Add(OptionKeys.ImageStretch, backgroundImageStretch);
                settings.Save();
            }
            else
            {
                BackgroundImageStretch = (Stretch)settings[OptionKeys.ImageStretch];
            }
            #endregion

            #region NotifyDays
            if (!settings.Contains(OptionKeys.NitifyDays))
            {
                settings.Add(OptionKeys.NitifyDays, notifyDays);
                settings.Save();
            }
            else
            {
                NotifyDays = int.Parse(settings[OptionKeys.NitifyDays].ToString());
            }
            #endregion
        }
        #endregion

        #region Event
        public event EventHandler SettingsChanged = delegate { };

        protected override void RaisePropertyChanged(string propertyName)
        {
            base.RaisePropertyChanged(propertyName);
            SettingsChanged.Invoke(this, new EventArgs());
        }

        public void Reset()
        {
            IsolatedStorageSettings.ApplicationSettings.Clear();
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.AutoSavePlace, true);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.ImageName, "/Omnia.Wallet;component/Assets/desert.png");
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.ImageStretch, Stretch.UniformToFill);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.Language, "zh-CN");
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.Location, ImageLocationValue.Applocation);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.NitifyDays, 7);
            IsolatedStorageSettings.ApplicationSettings.Save();

            CheckSettings();
        }
        #endregion

    }
}
